<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="130px">
      <!-- <el-form-item label="健身计划名称" prop="fitnessPlanName">
        <el-input
          v-model="queryParams.fitnessPlanName"
          placeholder="请输入健身计划名称"
          clearabl3
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="健身计划名称" prop="scheduleId">
        <el-select v-model="queryParams.scheduleId" placeholder="请输入健身计划名称" clearable>
          <el-option
            v-for="item in scheduleOptions"
            :key="item.scheduleId"
            :label="item.scheduleName"
            :value="item.scheduleId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用户名" prop="userId">
        <el-select v-model="queryParams.userId" placeholder="请输入用户名" clearable>
          <el-option
            v-for="item in userOptions"
            :key="item.userId"
            :label="item.nickName"
            :value="item.userId"
          />
        </el-select>
      </el-form-item>
      
      <!-- <el-form-item label="健身计划推荐名称" prop="scheduleId">
        <el-input
          v-model="queryParams.scheduleId"
          placeholder="请输入健身计划推荐名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item style="float: right;">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:fitnessplans:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:fitnessplans:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:fitnessplans:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:fitnessplans:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="fitnessplansList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column align="center" type="index" />
      <el-table-column label="健身计划名称" align="center" prop="scheduleId" >
        <template slot-scope="scope">
          <span>{{ scope.row.scheduleId?scheduleOptions.find(item => item.scheduleId == scope.row.scheduleId).scheduleName:'' }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="健身计划名称" align="center" prop="fitnessPlanName" /> -->
      <el-table-column label="用户名" align="center" prop="userId" >
        <template slot-scope="scope">
          <span>{{ scope.row.userId?userOptions.find(item => item.userId == scope.row.userId).nickName:'' }}</span>
        </template>
      </el-table-column>
      
      <el-table-column label="训练计划周期" align="center" prop="fitnessPlanDuration" />
      
      <!-- <el-table-column label="健身计划推荐名称" align="center" prop="scheduleId" /> -->
      <el-table-column label="计划开始时间" align="center" prop="fitnessPlanStarttime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fitnessPlanStarttime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="训练计划状态" align="center" prop="fitnessPlanState">
        <template slot-scope="scope">
          <template v-if="scope.row.fitnessPlanState === 'state1'">
            <dict-tag :options="dict.type.sys_plan_state" style="color:#F56C6C" :value="scope.row.fitnessPlanState"/>
          </template>
          <template v-if="scope.row.fitnessPlanState === 'state2'">
            <dict-tag :options="dict.type.sys_plan_state" style="color:#18e718" :value="scope.row.fitnessPlanState"/>
          </template>
          <template v-if="scope.row.fitnessPlanState === 'state3'">
            <dict-tag :options="dict.type.sys_plan_state" style="color:#686767" :value="scope.row.fitnessPlanState"/>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="fitnessPlanDescription" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:fitnessplans:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:fitnessplans:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改健身计划信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="健身计划名称" prop="scheduleId">
              <el-select v-model="form.scheduleId" placeholder="请输入健身计划名称" clearable style="width: 100%;">
                <el-option
                  v-for="item in scheduleOptions"
                  :key="item.scheduleId"
                  :label="item.scheduleName"
                  :value="item.scheduleId"
                />
              </el-select>
            </el-form-item>
            <!-- <el-form-item label="健身计划名称" prop="fitnessPlanName">
              <el-input v-model="form.fitnessPlanName" placeholder="请输入健身计划名称" />
            </el-form-item> -->
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户名" prop="userId">
              <el-select v-model="form.userId" placeholder="请输入用户名" clearable style="width: 100%;">
                <el-option
                  v-for="item in userOptions"
                  :key="item.userId"
                  :label="item.nickName"
                  :value="item.userId"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
        
          <el-col :span="12">
            <el-form-item label="训练计划周期" prop="fitnessPlanDuration">
              <el-input v-model="form.fitnessPlanDuration" placeholder="请输入训练计划周期" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划开始时间" prop="fitnessPlanStarttime">
              <el-date-picker clearable
                v-model="form.fitnessPlanStarttime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择计划开始时间" style="width: 100%;">
              </el-date-picker>
            </el-form-item>
            <!-- <el-form-item label="健身计划推荐名称" prop="scheduleId">
              <el-input v-model="form.scheduleId" placeholder="请输入健身计划推荐名称" />
            </el-form-item> -->
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="训练计划状态" prop="fitnessPlanState">
              <el-select v-model="form.fitnessPlanState" placeholder="请选择训练计划状态" style="width: 100%;">
                <el-option
                  v-for="dict in dict.type.sys_plan_state"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFitnessplans, getFitnessplans, delFitnessplans, addFitnessplans, updateFitnessplans } from "@/api/system/fitnessplans";
import { listUser } from "@/api/system/user"
import { listSchedule } from "@/api/system/schedule" 

export default {
  name: "Fitnessplans",
  dicts: ['sys_plan_state'],
  data() {
    return {
      //用户名
      userOptions:[],
      //健身计划推荐
      scheduleOptions :[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 健身计划信息表格数据
      fitnessplansList: [],
      //用户信息
      sysUserList:[],
      //推荐计划信息
      scheduleList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 训练计划状态时间范围
      daterangeFitnessPlanStarttime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fitnessPlanName: null,
        userId: null,
        scheduleId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fitnessPlanName: [
          { required: true, message: "健身计划名称不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户名  不能为空", trigger: "blur" }
        ],
        scheduleId: [
          { required: true, message: "健身计划推荐名称不能为空", trigger: "blur" }
        ],
        fitnessPlanState: [
          { required: true, message: "训练计划状态不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getUsers();
    this.getSchedule();
  },
  methods: {
    /** 查询用户信息 */
    getUsers(){
      const params = {
        pageNum:1,
        pageSize:0,
      }
      listUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.userOptions = response.rows
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 查询训练计划推荐 */
    getSchedule(){
      const params = {
        pageNum:1,
        pageSize:0,
      }
      listSchedule(this.queryParams).then(response => {
        this.scheduleOptions = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询健身计划信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeFitnessPlanStarttime && '' != this.daterangeFitnessPlanStarttime) {
        this.queryParams.params["beginFitnessPlanStarttime"] = this.daterangeFitnessPlanStarttime[0];
        this.queryParams.params["endFitnessPlanStarttime"] = this.daterangeFitnessPlanStarttime[1];
      }
      listFitnessplans(this.queryParams).then(response => {
        this.fitnessplansList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fitnessPlanId: null,
        fitnessPlanName: null,
        userId: null,
        fitnessPlanDescription: null,
        fitnessPlanDuration: null,
        scheduleId: null,
        fitnessPlanStarttime: null,
        fitnessPlanState: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.sysUserList = [];
      this.scheduleList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeFitnessPlanStarttime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.fitnessPlanId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加健身计划信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const fitnessPlanId = row.fitnessPlanId || this.ids
      getFitnessplans(fitnessPlanId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改健身计划信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.fitnessPlanId != null) {
            updateFitnessplans(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFitnessplans(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const fitnessPlanIds = row.fitnessPlanId || this.ids;
      this.$modal.confirm('是否确认删除健身计划信息编号为"' + fitnessPlanIds + '"的数据项？').then(function() {
        return delFitnessplans(fitnessPlanIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/fitnessplans/export', {
        ...this.queryParams
      }, `fitnessplans_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
